package com.earthchen.hbase.hos.core.config;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.InputStream;
import java.util.Properties;

/**
 * @author: EarthChen
 * @date: 2018/07/31
 */
public class HosConfig {

    private static HosConfig hosConfig;

    private static Properties properties;

    /**
     * 获取classpath所有的properties文件
     */
    static {
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        hosConfig = new HosConfig();
        try {
            hosConfig.properties = new Properties();
            Resource[] resources = resourcePatternResolver.getResources("classpath:*.properties");
            for (Resource resource : resources) {
                Properties prop = new Properties();
                InputStream inputStream = resource.getInputStream();
                prop.load(inputStream);
                inputStream.close();
                hosConfig.properties.putAll(prop);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private HosConfig() {

    }

    public static HosConfig getHosConfig() {
        return hosConfig;
    }

    /**
     * 获取字符串类型的值
     *
     * @param key
     * @return
     */
    public String getStrng(String key) {
        return properties.get(key).toString();
    }

    /**
     * 获取int类型的值
     *
     * @param key
     * @return
     */
    public int getInt(String key) {
        return Integer.parseInt(properties.get(key).toString());
    }


}
